package top.yehot.kettle.boot.controller;

import java.util.Arrays;
import java.util.List;

import javax.validation.Valid;

import org.pentaho.di.core.encryption.Encr;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import top.yehot.cmm.util.spring.anns.ResponseResult;
import top.yehot.kettle.boot.core.SysConst;
import top.yehot.kettle.boot.models.entity.Repository;
import top.yehot.kettle.boot.models.vo.repository.DatabaseInfoVO;
import top.yehot.kettle.boot.models.vo.repository.RepoAddVO;
import top.yehot.kettle.boot.models.vo.repository.RepoEditVO;
import top.yehot.kettle.boot.models.vo.repository.RepoSearchVO;
import top.yehot.kettle.boot.service.RepositoryService;

/**
 * 资源库管理
 * 
 * @author lquan
 * @date 2022年5月28日 下午6:53:34
 */
@Api(tags = "资源库管理")
@RestController
@RequestMapping(SysConst.VERSION + "/repository")
@AllArgsConstructor
@ResponseResult
public class RepositoryController {
	
	private final RepositoryService repositoryService;

	@ApiOperation("查询资源库列表")
	@PostMapping("list")
	public IPage<Repository> list(@RequestBody RepoSearchVO param) {
		return repositoryService.list(param);
	}
	
	@ApiOperation("测试资源库链接")
    @PostMapping("testConnection")
    public void testConnection(@RequestBody @Valid RepoAddVO param) {
        repositoryService.testConnection(null, param);
    }
	
	@ApiOperation("添加资源库")
	@PostMapping("add")
    public void add(@RequestBody @Valid RepoAddVO param) {
        repositoryService.add(param);
    }
	
	@ApiOperation("根据ID查询资源库")
	@GetMapping("getById")
	public RepoEditVO getById(@RequestParam("id") String id) {
		return repositoryService.getCusById(id);
	}
	
	@ApiOperation("更新资源库")
    @PostMapping("update")
    public void update(@RequestBody @Valid RepoEditVO repo) {
        repositoryService.update(repo);
    }
	
	@ApiOperation("根据id删除资源库")
    @GetMapping("delete")
    public void delete(@RequestParam("id") String id) {
        repositoryService.deleteBatch(Arrays.asList(id));
    }
	
	@ApiOperation("批量删除资源库")
	@PostMapping("deleteBatch")
    public void deleteBatch(@RequestBody List<String> ids) {
        repositoryService.deleteBatch(ids);
    }
	
	@ApiOperation("根据资源库id获取资源库下数据库连接")
    @GetMapping("getDatabasesByRepId")
    public List<DatabaseInfoVO> getDatabasesByRepId(@RequestParam("repId") String repId) {
		return repositoryService.getDatabasesByRepId(repId);
    }
	
	@ApiOperation("显示密码")
    @GetMapping("showPwd")
    public String showPwd(@RequestParam("pwd") String pwd) {
		return Encr.decryptPassword(pwd);
    }
	
	@ApiOperation("共享数据库")
	@GetMapping("sharedDb")
	public void sharedDb(@RequestParam("repId") String repId, @RequestParam("name") String name) {
		repositoryService.sharedDb(repId, name);
	}
	
	@ApiOperation("取消共享数据库")
	@GetMapping("closeSharedDb")
	public void closeSharedDb(@RequestParam("name") String name) {
		repositoryService.closeSharedDb(name);
	}
	
}
